begintownscript;

variables;
short creature_count = 0;
short choice = 0;
short pc_counter = 0;
short lead_char = 0;
short death_toll = 0;

body;

beginstate 0; //INIT_STATE
	set_crime_tolerance(2);

	//Checks for living & present characters
	//Miali
	if(get_flag(10,1) == 0)
		activate_hidden_group(1);

	//iracos
	if((get_flag(10,5) == 0) && (get_flag(14,0) != 3))
		activate_hidden_group(5);

	//Will
	if(get_flag(10,6) == 0)
		activate_hidden_group(6);
	//Mitch
	if(get_flag(14,3) == 1)
		set_flag(10,7,2);
	if(get_flag(10,7) == 0)
		activate_hidden_group(7);
	//Mayor Anderson
	if((get_flag(5,7) == 1) && (get_flag(5,8) < 2))
		set_flag(10,9,2);
	if(get_flag(10,9) == 0)
		activate_hidden_group(9);

	//Names & Levels
	set_name(6,"Karl");
	set_name(7,"Mayor Anderson");
	set_name(8,"Iracos");
	set_name(9,"Samuel");
	set_name(11,"Mitch");
	set_name(12,"Will");
	set_name(14,"Miali");
	set_name(15,"Spark");

	//Remove bodies from ground
	if(get_flag(4,9) == 2)
		set_terrain(9,14,0);
	if(get_flag(4,4) == 2)
		set_terrain(29,32,0);
	if(get_flag(4,23) == 2)
		set_terrain(37,26,0);
	if(get_flag(4,24) == 2)
		set_terrain(22,22,0);
	if(get_flag(4,22) == 2)
		set_terrain(6,31,0);

	//gravestones
	if(get_flag(4,21) >= 1)
		set_terrain(1,1,486);
	if(get_flag(4,21) >= 2)
		set_terrain(3,1,486);
	if(get_flag(4,21) >= 3)
		set_terrain(1,3,486);
	if(get_flag(4,21) >= 4)
		set_terrain(3,3,486);
	if(get_flag(4,21) >= 5)
		set_terrain(1,5,486);
	if(get_flag(4,21) == 6)
		set_terrain(3,5,486);

	
	//Statues in city hall
	if(get_flag(14,6) >= 1)
		set_terrain(22,27,284);
	if(get_flag(14,7) == 1)
		set_terrain(20,20,490);
	if(get_flag(14,8) == 1)
		set_terrain(22,20,490);
	if(get_flag(14,9) == 1)
		set_terrain(20,27,490);


	if(get_flag(4,0) == 0) {
		reset_dialog();
		add_dialog_str(0,"And now, you see the destruction up close for the first time. Truth be told, it's not as bad as it looked from a distance, as bits of many buildings are still standing, though most look ready to collapse.",0);
		add_dialog_str(1,"At this end of the town, what once served as an open plaza has become a makeshift meeting place for survivors. A fire burns merrily in the center of the plaza, contrasting sharply with the ruined and scorched buildings.",0);
		add_dialog_str(2,"The survivors themselves look like the shock hasn't worn off yet, and understandably so. Some are asleep, some are sobbing, and some are just staring into the fire, just looking defeated. A few of them notice your arrival, but fewer seem to care.",0);
		add_dialog_str(3,"You've got a bad feeling that a lot remains to be done here.",0);
		run_dialog(1);		
		set_flag(4,0,1);

		if(get_flag(20,0) == 1) {
			reset_dialog();
			add_dialog_str(0,"As you approach the circle of survivors, you overhear one of them talking about a hurried-looking soldier who rushed into the ruins a short while ago.",0);
			if(get_flag(2,13) >= 2)
				add_dialog_str(2,"You have a sinking feeling in your gut that it's Private Stirlen. Perhaps it would be a good idea to try and find him, before he can do something he'll regret.",0);
			else
				add_dialog_str(2,"It's probably the same soldier that nearly knocked you over on your way here. You wonder what he could be up to.",0);
			run_dialog(1);
			}
		}

	if((get_flag(4,27) == 0) && (get_flag(5,8) < 2) && (get_flag(5,7) == 1)) {
		force_instant_terrain_redraw();
		pause(15);
		message_dialog("That's odd... you can't see the mayor. You weren't down there for too long...","This is a little suspicious.");
		set_flag(4,27,1);
		}

	//Dialogue Pics
	set_char_dialogue_pic(7,1949,0);
	set_char_dialogue_pic(14,1947,0);
	set_char_dialogue_pic(11,1962,0);
	set_char_dialogue_pic(12,1922,0);
	set_char_dialogue_pic(8,1945,0);
	set_char_dialogue_pic(9,1957,0);
	set_char_dialogue_pic(6,1958,0);


break;


beginstate 1; //EXIT_STATE

break;


beginstate 2; //START_STATE
	pc_counter = 0;
	while(char_ok(pc_counter) == 0) {
		pc_counter = pc_counter + 1;
		}
	lead_char = pc_counter;

break;


beginstate 5; //exit dialog for first time, only when actually leaving

	if(town_status(4) == 2) {
		reset_dialog();
		add_dialog_str(0,"As you leave the ruins behind you, you begin to feel a bit of regret. You've clearly been marked as a criminal, which will make completing your quest impossible.",0);
		add_dialog_str(1,"The Empire will send troops once it hears that one of its towns was attacked, so you decide to flee while you still can.",0);
		add_dialog_str(2,"You slip out of the valley, unnoticed and never to return.",0);
		run_dialog(1);
		end_scenario(0);
		}

	if(get_flag(4,0) == 1) {
		reset_dialog();
		add_dialog_str(0,"Putting the ruins behind you, you're not sure what to do. There's clearly something behind all of this, but you're no closer to figuring it out.",0);
		add_dialog_str(1,"Then, a thought strikes you. If there's anybody in the valley that will know what's going on, it'll be the Empire.",0);
		if(get_flag(2,6) == 2)
			add_dialog_str(2,"Who knows? Maybe they won't lock you out this time.",0);
		run_dialog(1);
		set_flag(4,0,2);
		}

break;


beginstate 10; //Crater dialog

	if(get_flag(14,5) == 0) {
		reset_dialog();
		add_dialog_str(0,"Well, at least you know where the blasts came from... this crater definitely wasn't here before. The bottom is filled with debris, and you see what appears to be a few shards of metal embedded in the sides.",0);
		add_dialog_str(1,"It looks like whoever set off these blasts buried them, if shallowly.",0);
		run_dialog(1);
		set_flag(14,5,1);
	}

break;


beginstate 11; //the fire

	message_dialog("You lean in to search the fire, and you burn yourselves.","Well, that was stupid.");
	damage_near_loc(6,24,10,1,1);

break;


beginstate 12; //the docks

	if(get_flag(14,7) == 0) {
		message_dialog("These docks haven't been hit as hard as the rest of the town. It just looks that a few sections were knocked into the lake by debris.","Any boats that may have been docked here are gone.");
		set_flag(14,7,1);
	}

break;


beginstate 20; //Misc. Townswoman 1's house

	reset_dialog();
	if(get_flag(4,3) == 0) {
		add_dialog_str(0,"This building looks like it's about ready to collapse. All of the walls are scorched and cracked, and a thick layer of rubble covers the floor. A body lies in the middle of the wreckage.",0);
		run_dialog(1);
		set_flag(4,3,1);
		}

break;



beginstate 21; //The body of Misc. Townswoman 1's husband, specobj trigger

	reset_dialog();
	if(get_flag(4,4) == 0) {
		add_dialog_str(0,"This body is charred beyond recognition. It looks like whoever this was might've been wearing some sort of armor, but it's been melted and warped beyond use.",0);
		add_dialog_str(1,"Of course, you'd never even think of stealing the armor off of a freshly dead innocent.",0);
		set_flag(4,4,1);
		if(get_flag(4,5) == 1) {
			add_dialog_str(2,"Do you take the body back?",0);
			add_dialog_choice(0, "Yes.");
			add_dialog_choice(1, "No.");
			}

		choice = run_dialog(1);
		if((get_flag(4,5) == 1) && (choice == 1)) {
			reward_give(446);
			set_terrain(29,32,0);
			set_flag(4,4,2);
			}
		}

	else if(get_flag(4,4) == 1) {
		add_dialog_str(0,"The body is still here, and it's beginning to smell pretty bad.",0);
		if(get_flag(4,5) == 1) {
			add_dialog_str(2,"Do you take the body back?",0);
			add_dialog_choice(0, "Yes.");
			add_dialog_choice(1, "No.");
			}

		choice = run_dialog(1);
		if((get_flag(4,5) == 1) && (choice == 1)) {
			reward_give(446);
			set_terrain(29,32,0);
			set_flag(4,4,2);
			}
		}
	else if(get_flag(4,4) == 2) {
		end();
		}

break;


beginstate 22; //finding Misc. Townswoman's necklace
	       //trigger: specobj script on a debris pile

	if(get_flag(14,1) == 0) {
		reset_dialog();
		add_dialog_str(0,"Sifting through the debris in this shattered house, you're surprised to find an intact necklace. Somehow, it still sparkles despite the ash and dust that coat it.",0);
		if(get_flag(4,2) == 1)
			add_dialog_str(1,"This might be the necklace that you were asked to find for that distraught townswoman. You feel like you should return it... even though it looks rather valuable.",0);
		if(get_skill_total(41) >= 30)
			add_dialog_str(2,"Thanks to your knowledge of item lore, you can tell that the necklace has some sort of enchantment on it. That'd make it even more valuable...",0);
		else
			add_dialog_str(2,"And for some reason, just being near the charm makes you feel a bit more cheerful, despite the wreckage that surrounds you.",0);
		run_dialog(1);
		set_flag(14,1,1);
	}

break;


beginstate 30; //Enter Sunken Cow again

	if(get_flag(4,7) == 0) {
		reset_dialog();
		add_dialog_str(0,"As you re-enter what was once the Sunken Cow, your immediate reaction is to look up at what remains of the roof. Some of the rafters still look stable, and those that look unstable aren't supporting anything anymore.",0);
		add_dialog_str(1,"Piles of roof tiles cover the floor, along with a few fallen timbers. Tables have been crushed, chairs have been splintered, and countless bottles have been broken.",0);
		if(party_can_see_loc(9,14) == 1)
			add_dialog_str(2,"There's even a body lying on the floor, which you could've detected by smell alone.",0);
		else
			add_dialog_str(2,"And while you can't see a body in here, you can definitely smell one. Sadly, you're almost growing accustomed to the smell of scorched flesh.",0);
		run_dialog(1);
		set_flag(4,7,1);
		}

break;


beginstate 31; //Trapdoor in the inn

	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't use the trapdoor in combat mode.",1);
		end();
		}

	reset_dialog();
	if(get_flag(4,8) == 0) {
		add_dialog_str(0,"This trapdoor, while thoroughly blackened, appears to be intact... except that the lock was knocked off the door by the blast. It's anybody's guess as to what lies below the inn.",0);
		set_flag(4,8,1);
		}
	add_dialog_str(1, "Do you descend to the inn's cellar?",0);
	add_dialog_choice(0,"Yes.");
	add_dialog_choice(1,"No.");
	choice = run_dialog(1);
	
	if(choice == 1) {
		set_flag(5,1,1);
		move_to_new_town(5,6,8);
		}
	else
		block_entry(1);
	
break;

beginstate 32; //Body in the inn

	reset_dialog();
	if(get_flag(4,9) == 0) {
		add_dialog_str(0,"This body is charred beyond recognition. Anything the person might've been carrying has either been crushed or burned.",0);
		set_flag(4,9,1);
		if(get_flag(4,5) == 1) {
			add_dialog_str(2,"Do you take the body back?",0);
			add_dialog_choice(0, "Yes.");
			add_dialog_choice(1, "No.");
			}

		choice = run_dialog(1);
		if((get_flag(4,5) == 1) && (choice == 1)) {
			reward_give(446);
			set_terrain(9,14,0);
			set_flag(4,9,2);
			}
		}

	else if(get_flag(4,9) == 1) {
		add_dialog_str(0,"The body is still here, and it's beginning to smell pretty bad.",0);
		if(get_flag(4,5) == 1) {
			add_dialog_str(2,"Do you take the body back?",0);
			add_dialog_choice(0, "Yes.");
			add_dialog_choice(1, "No.");
			}

		choice = run_dialog(1);
		if((get_flag(4,5) == 1) && (choice == 1)) {
			reward_give(446);
			set_terrain(9,14,0);
			set_flag(4,9,2);
			}
		}
	else if(get_flag(4,9) == 2) {
		end();
		}

break;



beginstate 41; //Body in the southwesternmost house

	reset_dialog();
	if(get_flag(4,22) == 0) {
		add_dialog_str(0,"This body is charred beyond recognition. Anything the person might've been carrying has either been crushed or burned.",0);
		set_flag(4,22,1);
		if(get_flag(4,5) == 1) {
			add_dialog_str(2,"Do you take the body back?",0);
			add_dialog_choice(0, "Yes.");
			add_dialog_choice(1, "No.");
			}

		choice = run_dialog(1);
		if((get_flag(4,5) == 1) && (choice == 1)) {
			reward_give(446);
			set_terrain(6,31,0);
			set_flag(4,22,2);
			}
		}

	else if(get_flag(4,22) == 1) {
		add_dialog_str(0,"The body is still here, and it's beginning to smell pretty bad.",0);
		if(get_flag(4,5) == 1) {
			add_dialog_str(2,"Do you take the body back?",0);
			add_dialog_choice(0, "Yes.");
			add_dialog_choice(1, "No.");
			}

		choice = run_dialog(1);
		if((get_flag(4,5) == 1) && (choice == 1)) {
			reward_give(446);
			set_terrain(6,31,0);
			set_flag(4,22,2);
			}
		}
	else if(get_flag(4,22) == 2) {
		end();
		}

break;


beginstate 50; //entering the easternmost house

	if(get_flag(14,8) == 0) {
		reset_dialog();
		add_dialog_str(0,"It's pretty easy to tell what happened to this house. The crater out by the southwest corner explains everything.",0);
		add_dialog_str(1,"It seems like the blast here actually carved a hole straight through the building, and that the rest of the damage was collateral.",0);
		add_dialog_str(2,"And to make the scene even more dour, there's a body in the middle of the wreckage.",0);
		run_dialog(1);
		set_flag(14,8,1);
	}

break;


beginstate 51; //Body in the easternmost house

	reset_dialog();
	if(get_flag(4,23) == 0) {
		add_dialog_str(0,"This body is charred beyond recognition. It looks like it was wearing armor before. Regrettably, the armor is ruined.",0);
		set_flag(4,23,1);
		if(get_flag(4,5) == 1) {
			add_dialog_str(2,"Do you take the body back?",0);
			add_dialog_choice(0, "Yes.");
			add_dialog_choice(1, "No.");
			}

		choice = run_dialog(1);
		if((get_flag(4,5) == 1) && (choice == 1)) {
			reward_give(448);
			set_terrain(37,26,0);
			set_flag(4,23,2);
			}
		}

	else if(get_flag(4,23) == 1) {
		add_dialog_str(0,"The body is still here, and it's beginning to smell pretty bad.",0);
		if(get_flag(4,5) == 1) {
			add_dialog_str(2,"Do you take the body back?",0);
			add_dialog_choice(0, "Yes.");
			add_dialog_choice(1, "No.");
			}

		choice = run_dialog(1);
		if((get_flag(4,5) == 1) && (choice == 1)) {
			reward_give(448);
			set_terrain(37,26,0);
			set_flag(4,23,2);
			}
		}
	else if(get_flag(4,23) == 2) {
		end();
		}

break;


beginstate 60; //entering city hall ruins

	if(get_flag(14,12) == 0) {
		reset_dialog();
		if(get_flag(0,14) == 1)
			add_dialog_str(0,"It's not a huge surprise, but the city hall was hit the worst. It's barely even a shell at this point.",0);
		else
			add_dialog_str(0,"This building was probably important, judging from how completely it was destroyed. Your best guess is that it was the city hall, in part due to the charred Empire emblem you spot in the rubble.",0);
		add_dialog_str(1,"In fact, parts of the building are still smoking. You'd be worried, if only there was anything substantial left to fall on you.",0);
		run_dialog(1);
		set_flag(14,12,1);
	}

break;


beginstate 61; //Body in the city hall ruins

	reset_dialog();
	if(get_flag(4,24) == 0) {
		if(get_flag(0,14) == 1)
			add_dialog_str(0,"This body has been charred beyond recognition, but you would guess that it's the Empire official you saw waiting to meet with the mayor.",0);
		else
			add_dialog_str(0,"This body is charred beyond recognition. Anything the person might've been carrying has either been crushed or burned.",0);
	if((get_flag(0,20) >= 2) || (get_flag(4,16) >= 0))
		add_dialog_str(1,"You're never going to be sure exactly why the mayor disliked this man. Perhaps it's for the best.",0);
		set_flag(4,24,1);
		if(get_flag(4,5) == 1) {
			add_dialog_str(2,"Do you take the body back?",0);
			add_dialog_choice(0, "Yes.");
			add_dialog_choice(1, "No.");
			}

		choice = run_dialog(1);
		if((get_flag(4,5) == 1) && (choice == 1)) {
			reward_give(446);
			set_terrain(22,22,0);
			set_flag(4,24,2);
			}
		}

	else if(get_flag(4,24) == 1) {
		add_dialog_str(0,"The body is still here, and it's beginning to smell pretty bad.",0);
		if(get_flag(4,5) == 1) {
			add_dialog_str(2,"Do you take the body back?",0);
			add_dialog_choice(0, "Yes.");
			add_dialog_choice(1, "No.");
			}

		choice = run_dialog(1);
		if((get_flag(4,5) == 1) && (choice == 1)) {
			reward_give(446);
			set_terrain(22,22,0);
			set_flag(4,24,2);
			}
		}
	else if(get_flag(4,24) == 2) {
		end();
		}

break;


beginstate 62; //the statue of Anderson

	if(get_flag(14,6) == 0) {
		reset_dialog();
		add_dialog_str(0,"This probably used to be a statue, but the blast smoothed any distinguishing features away.",0);
		add_dialog_str(1,"As you look closer, you notice what appears to be a hinge. It joins what may have been a finger to what was probably an arm.",0);
		add_dialog_str(2,"Out of curiosity, you poke it. The entire statue crumbles into dust, revealing a trapdoor under what used to be the statue's base.",0);
		run_dialog(1);
		play_sound(-108);
		set_terrain(22,27,495);
		force_instant_terrain_redraw();
		pause(1);
		set_terrain(22,27,496);
		force_instant_terrain_redraw();
		pause(1);
		set_terrain(22,27,497);
		force_instant_terrain_redraw();
		pause(1);
		set_terrain(22,27,498);
		force_instant_terrain_redraw();
		pause(1);
		set_terrain(22,27,284);
		force_instant_terrain_redraw();
		set_flag(14,6,1);
		}

break;


beginstate 63; //Trapdoor in the city hall ruins

	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't use the trapdoor in combat mode.",1);
		end();
		}

	if(get_flag(5,7) == 1) {
		message_dialog("The trapdoor is still here, but you no longer have a compelling reason to descend. So you don't.","");
		end();
		}

	if(get_flag(14,6) > 0) {
		reset_dialog();
		if(get_flag(4,25) == 0) {
			add_dialog_str(0,"Under a thin layer of rubble from the crumbled statue, you find a trapdoor with an impressive looking lock on it.",0);
			set_flag(4,25,1);
			if(has_special_item(3) == 0) {
				add_dialog_str(1,"Regrettably, you don't seem to have a key that fits it.",0);
				run_dialog(1);
				end();
				}
			}
		else if (has_special_item(3) == 0) {
			add_dialog_str(2,"The trapdoor is still here, and you still don't have the key to open it.",0);
			run_dialog(1);
			end();
			}
		add_dialog_str(3, "Do you descend?",0);
		add_dialog_choice(0,"Yes.");
		add_dialog_choice(1,"No.");
		choice = run_dialog(1);
	
		if(choice == 1) {
			set_flag(5,1,4);
			move_to_new_town(5,21,27);
			}
		else
			block_entry(1);	
		}
break;


beginstate 70; //entering Will's house, adjacent building

	if(get_flag(14,11) == 0) {
		message_dialog("This building... well, it looks like is used to be two houses. Now, it's just one big empty shell, with a few fragments of wall supporting a few meager sections of roof.","Debris coats the floor, and it still smolders in a few places.");
		set_flag(14,11,1);
	}

break;



beginstate 80; //Enter storeroom

	if(get_flag(14,10) == 0) {
		if(get_flag(0,19) == 1)
			message_dialog("The town storeroom has been completely torched, most likely due to the crater outside the east wall. The only thing left standing is a fragile looking table.","A few piles of debris are still smoking. They smell bad.");
		else
			message_dialog("Everything in this room appears to have been torched. Most likely due to the huge crater outside the east wall.","A few piles of debris are still smoking. They smell bad.");
		set_flag(14,10,1);
	}

break;


beginstate 90; //enter the forge

	if(get_flag(14,9) == 0) {
		reset_dialog();
		add_dialog_str(0,"This building has been just about flattened. A few tiny segments of wall remain standing, including some blown-out windows. The only thing left intact is an anvil.",0);
		if(get_flag(0,17) == 1) {
			add_dialog_str(1,"The floor seems to have sunken a bit in places, but it still feels warm. So, if nothing else, the forge is probably intact.",0);
			}
		else {
			add_dialog_str(1,"The floor seems to have sunken a bit in places, and it feels warm for some reason.",0);
			}
		run_dialog(1);
		set_flag(14,9,1);
	}

break;


beginstate 91; //Trapdoor in the forge

	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't use the trapdoor in combat mode.",1);
		end();
		}

	reset_dialog();
	if(get_flag(4,26) == 0) {
		if(get_flag(0,17) == 1)
			add_dialog_str(0,"This trapdoor, while thoroughly blackened, appears to be intact, except that the lock was knocked off the door by the blast. If you recall correctly, there's a forge under here.",0);
		else
			add_dialog_str(0,"This trapdoor, while thoroughly blackened, appears to be intact... except that the lock was knocked off the door by the blast. It's anybody's guess as to what lies below it.",0);

		if(get_flag(20,0) == 1)
			add_dialog_str(1,"Strange... there are footprints in the dust nearby, and it looks like someone used the trapdoor recently.",0);
		set_flag(4,26,1);
		}
	add_dialog_str(2, "Do you descend?",0);
	add_dialog_choice(0,"Yes.");
	add_dialog_choice(1,"No.");
	choice = run_dialog(1);
	
	if(choice == 1) {
		set_flag(5,1,2);
		move_to_new_town(5,26,13);
		}
	else
		block_entry(1);
	
break;
